package com.lcxsoft.cartservice.controller;


import com.lcxsoft.cartservice.query.CartAddQuery;
import com.lcxsoft.cartservice.service.CartService;
import com.lcxsoft.cartservice.url.CartServiceUrl;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.feign.clients.GoodsClient;
import com.lcxsoft.feign.entity.Goods;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2022-01-21
 */
@RestController
@RequestMapping(CartServiceUrl.packageUrl)
public class CartController {

    @Resource
    private CartService cartService;

    /**
     * 把商品添加到购物车
     * @param query
     * @param request
     * @return
     */
    @RequestMapping(value = CartServiceUrl.CustomerUserUrl.addCart, method = RequestMethod.POST)
    public R addCart(CartAddQuery query, HttpServletRequest request){ return cartService.addToCartByUserId(query, request); }

    /**
     * 查询购物车列表
     * @param request
     * @return
     */
    @RequestMapping(value = CartServiceUrl.CustomerUserUrl.queryCartList, method = RequestMethod.POST)
    public R queryCartList(HttpServletRequest request){
        return cartService.findCartList(request);
    }

    /**
     * 减少购物车中商品数量，如果数量为0是从购物车中删除
     * @param cartId
     * @return
     */
    @RequestMapping(value = CartServiceUrl.CustomerUserUrl.reduceCartAmount, method = RequestMethod.POST)
    public R reduceCartAmount(String cartId){
        return cartService.decreaseCartAmount(cartId);
    }

    @RequestMapping(value = CartServiceUrl.CustomerUserUrl.removeCartById, method = RequestMethod.POST)
    public R removeCartById(String cartId){
        return cartService.removeById(cartId) ? R.ok().message("操作成功") : R.error().message("操作失败");
    }


}

